//
//  IGMyArtViewController.m
//  jingdongfang
//
//  Created by MTiMac02 on 2018/5/7.
//  Copyright © 2018年 BOE-SBG-CTO. All rights reserved.
//

#import "IGMyArtViewController.h"
#import "MyDrawcell.h"
#import "IGMyiGalleryVC.h"
#import "ArtGalleryViewController.h"
#import "IGMylikeViewController.h"
#import "IGCreatPictureListVC.h"
#import "IGMoreWorksSelectVC.h"
#import "IGMyCreateDrawApi.h"
#import "IGMyFavDrawApi.h"
#import "CollectViewController.h"
#import "IGPictureListDetailVC.h"
#import "IGMyPaintingListManagerVC.h"
#import "IGSimilarArtVC.h"

@interface IGMyArtViewController ()
{
    UIButton *downBtn;
    NSString *likeDrawNum;
    NSString *createDrawNum;
}
@property(nonatomic,strong)IGMyCreateDrawApi *createApi;
@property(nonatomic,strong)IGMyFavDrawApi *favApi;
@property (nonatomic, strong)NSMutableArray *cDataArr;
@property (nonatomic, strong)NSMutableArray *fDataArr;
@property (nonatomic, copy) NSString *cPageNum;
@property (nonatomic, copy) NSString *fPageNum;
@end

@implementation IGMyArtViewController
#pragma mark-懒加载
-(NSMutableArray *)cDataArr{
    if (!_cDataArr) {
        _cDataArr=[NSMutableArray array];
    }
    return _cDataArr;
}
-(NSMutableArray *)fDataArr{
    if (!_fDataArr) {
        _fDataArr=[NSMutableArray array];
    }
    return _fDataArr;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title=@"我的艺术库";
    self.tableView.backgroundColor=[UIColor mainBackgroundColor];
    [self.tableView setTableFooterView:[[UIView alloc]initWithFrame:CGRectZero]];
    [self ig_setupRightButtonItemWithTitle:nil image:[UIImage imageNamed:@"iGallery_Bind_Slave1"] highLightedImage:[UIImage imageNamed:@"iGallery_Bind_Slave1"]];
    [self.tableView registerNib:[UINib nibWithNibName:@"MyDrawcell" bundle:nil] forCellReuseIdentifier:@"MyDrawcell"];
    self.createApi = [IGMyCreateDrawApi new];
    self.favApi=[IGMyFavDrawApi new];
    _cPageNum=@"1";
    _fPageNum=@"1";
    [self createData];
    [self favData];
}
-(void)viewWillAppear:(BOOL)animated{
    self.createApi = [IGMyCreateDrawApi new];
    self.favApi=[IGMyFavDrawApi new];
    _cPageNum=@"1";
    _fPageNum=@"1";
    [self createData];
    [self favData];
}
-(void)createData{
    self.createApi.pageNum=_cPageNum;
    @KWeakObj(self)
    [self.createApi startWithDecodeClass:[IGMyCreateDrawResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
        if (responseObject) {
            IGMyCreateDrawResp *resp=responseObject;
            IGMyCreateDrawModel *model=resp.data;
            createDrawNum=model.drawlistSize;
            if ([_cPageNum isEqualToString:@"1"])
            {
                _cDataArr = [NSMutableArray arrayWithArray:model.drawlist];
            }
            else
            {
                [_cDataArr addObjectsFromArray:model.drawlist];
            }
            [selfWeak.tableView reloadData];
        }
    }];
}
-(void)favData{
    self.favApi.pageNum=_fPageNum;
    @KWeakObj(self)
    [self.favApi startWithDecodeClass:[IGMyFavDrawResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
        if (responseObject) {
            IGMyFavDrawResp *resp=responseObject;
            IGMyFavDrawModel *model=resp.data;
            if (model.drawlist&&model.drawlist.count>0) {
                [selfWeak setupFooter];
            }else{
                [selfWeak.tableView ig_endFooterRefreshingWithNoMoreData];
            }
            likeDrawNum=model.drawlistSize;
            if ([_fPageNum isEqualToString:@"1"])
            {
                _fDataArr = [NSMutableArray arrayWithArray:model.drawlist];
            }
            else
            {
                [_fDataArr addObjectsFromArray:model.drawlist];
            }
            [selfWeak.tableView reloadData];
        }
    }];
}
- (void)setupFooter{
    @KWeakObj(self)
    [self.tableView ig_setFooterWithRefreshingBlock:^{
        _fPageNum= [NSString stringWithFormat:@"%d", [_fPageNum intValue] + 1];
        [selfWeak favData];
    }];
}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section==0) {
        return 4;
    }
    if (section==1) {
        return _cDataArr.count?_cDataArr.count:0;
    }
    if (section==2) {
        return _fDataArr.count?_fDataArr.count:0;
    }
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.separatorInset = UIEdgeInsetsMake(0, 44, 0, 0);
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    cell.textLabel.font = [UIFont font16];
    cell.detailTextLabel.font = [UIFont font12];
    cell.textLabel.textColor=[UIColor c4Color];
    cell.textLabel.textAlignment=NSTextAlignmentLeft;
    UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    img.center = CGPointMake(25, 55/2);
    img.contentMode = UIViewContentModeScaleAspectFit;
    if (indexPath.section==0) {
        if (indexPath.row==0) {
            cell.textLabel.text = @"我的收藏";
            img.image=[UIImage imageNamed:@"CompoundShape"];
            [cell.contentView addSubview:img];
        } else if (indexPath.row==1) {
            cell.textLabel.text = @"我的上传";
            img.image=[UIImage imageNamed:@"cload-upload"];
            [cell.contentView addSubview:img];
        }else if (indexPath.row==2) {
            cell.textLabel.text = @"我的关注";
            img.image=[UIImage imageNamed:@"hearth"];
            [cell.contentView addSubview:img];
        }else if (indexPath.row==3) {
            cell.textLabel.text = @"我的推送";
            img.image=[UIImage imageNamed:@"gupload"];
            [cell.contentView addSubview:img];
        }
    }
    if (indexPath.section==1) {
        MyDrawcell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyDrawcell" forIndexPath:indexPath];
        if (!cell) {
            cell = [[MyDrawcell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MyDrawcell"];
        }
        cell.data=_cDataArr[indexPath.row];
        cell.Like=NO;
        return cell;
    }
    if (indexPath.section==2) {
        MyDrawcell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyDrawcell" forIndexPath:indexPath];
        if (!cell) {
            cell = [[MyDrawcell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MyDrawcell"];
        }
        cell.data=_fDataArr[indexPath.row];
        cell.Like=YES;
        return cell;
    }
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0) {
        if (indexPath.row==0) {
            [UIViewController ig_pushViewControllerClass:[CollectViewController class] animated:YES];
        }else if (indexPath.row==1){
            [UIViewController ig_pushViewControllerClass:[ArtGalleryViewController class] animated:YES];
        }else if (indexPath.row==2){
            [UIViewController ig_pushViewControllerClass:[IGMylikeViewController class] animated:YES];
        }else if (indexPath.row==3){
            [UIViewController ig_pushViewControllerClass:[IGMyiGalleryVC class] animated:YES];
        }
    } else if (indexPath.section == 1) {
        IGDrawModel *model = _cDataArr[indexPath.row];
        IGPictureListDetailVC *vc = [[IGPictureListDetailVC alloc] init];
        vc.drawlistId = model.drawId;
        [UIViewController ig_pushViewController:vc animated:YES];
    } else if (indexPath.section == 2) {
        IGDrawModel *model = _cDataArr[indexPath.row];
        IGPictureListDetailVC *vc = [[IGPictureListDetailVC alloc] init];
        vc.drawlistId = model.drawId;
        [UIViewController ig_pushViewController:vc animated:YES];
    }
    
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0) {
        return 55;
    }
    return 65;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section==1||section==2) {
        return 33;
    }
    return 0.01f;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section==1) {
        if(_cDataArr.count%3==0&&_cDataArr.count<[createDrawNum integerValue]){
            return 30;
        }
        return 0.01f;
    }
    return 0.01f;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section==1) {
        UIView *gray = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, 33)];
        gray.backgroundColor = [UIColor c2Color];
        UILabel *myArtLable=[[UILabel alloc]initWithFrame:CGRectMake(15, 0, KSCREENWIDTH, 33)];
        myArtLable.text=[NSString stringWithFormat:@"我创建的画单(%@)",createDrawNum?createDrawNum:@"0"];
        myArtLable.textAlignment=NSTextAlignmentLeft;
        myArtLable.font=[UIFont systemFontOfSize:15];
        myArtLable.textColor = [UIColor c4Color];
        myArtLable.backgroundColor = [UIColor clearColor];
        [gray addSubview:myArtLable];
        //添加画单
        UIButton *add=[UIButton buttonWithType:UIButtonTypeCustom];
        add.frame=CGRectMake(KSCREENWIDTH, 0, 30, 30);
        [add setImage:[UIImage imageNamed:@"newart"] forState:UIControlStateNormal];
        [add setImage:[UIImage imageNamed:@"newart"] forState:UIControlStateSelected];
        [add addTarget:self action:@selector(addClick) forControlEvents:UIControlEventTouchUpInside];
        [gray addSubview:add];
        //管理画单
        UIButton *manage=[UIButton buttonWithType:UIButtonTypeCustom];
        manage.frame=CGRectMake(0, 0, 30, 30);
        [manage setImage:[UIImage imageNamed:@"manageart"] forState:UIControlStateNormal];
        [manage setImage:[UIImage imageNamed:@"manageart"] forState:UIControlStateSelected];
        [manage addTarget:self action:@selector(manageClick) forControlEvents:UIControlEventTouchUpInside];
        [gray addSubview:manage];
        add.x=KSCREENWIDTH-14-2*add.width-11;
        add.centerY=gray.centerY;
        manage.x=add.right+11;
        manage.centerY=add.centerY;
        return gray;
    }else if (section==2) {
        UIView *gray2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, 33)];
        gray2.backgroundColor = [UIColor c2Color];
        UILabel *myLikeLable=[[UILabel alloc]initWithFrame:CGRectMake(15, 0, KSCREENWIDTH, 33)];
        myLikeLable.text=[NSString stringWithFormat:@"我喜爱的画单(%@)",likeDrawNum?likeDrawNum:@"0"];
        myLikeLable.textAlignment=NSTextAlignmentLeft;
        myLikeLable.font=[UIFont systemFontOfSize:15];
        myLikeLable.textColor = [UIColor c4Color];
        myLikeLable.backgroundColor = [UIColor clearColor];
        [gray2 addSubview:myLikeLable];
        //管理画单
        UIButton *manage1=[UIButton buttonWithType:UIButtonTypeCustom];
        manage1.frame=CGRectMake(0, 0, 30, 30);
        [manage1 setImage:[UIImage imageNamed:@"manageart"] forState:UIControlStateNormal];
        [manage1 setImage:[UIImage imageNamed:@"manageart"] forState:UIControlStateSelected];
        [manage1 addTarget:self action:@selector(manageLikeClick) forControlEvents:UIControlEventTouchUpInside];
        [gray2 addSubview:manage1];
        manage1.x=KSCREENWIDTH-14-manage1.width;
        manage1.centerY=gray2.centerY;
        return gray2;
    }
    return nil;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    if (section==1) {
        UIView *down= [[UIView alloc] initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, 30)];
        down.backgroundColor = [UIColor whiteColor];
        downBtn=[UIButton buttonWithType:UIButtonTypeCustom];
        downBtn.frame=CGRectMake(0, 0, 100, 30);
        downBtn.backgroundColor=[UIColor clearColor];
        downBtn.titleLabel.font=[UIFont font12];
        [downBtn setTitle:@"显示更多画单"forState:UIControlStateSelected];
        [downBtn setTitle:@"显示更多画单" forState:UIControlStateNormal];
        [downBtn setTitleColor:[UIColor c5Color] forState:UIControlStateNormal];
        [downBtn addTarget:self action:@selector(moreClick) forControlEvents:UIControlEventTouchUpInside];
        downBtn.centerX=down.centerX;
        downBtn.centerY=down.centerY;
        [down addSubview:downBtn];
        return down;
    }
    return nil;
}

- (void)ig_rightButtonPressed:(id)sender {
    [UIViewController ig_pushViewControllerClass:[IGMyiGalleryVC class] animated:YES];
}

- (void)addClick {
    [UIViewController ig_pushViewControllerClass:[IGCreatPictureListVC class] animated:YES];
}

- (void)manageClick{
    if (_cDataArr.count==1) {
        [self.tableView presentMessageTips:@"此画单不可删除"];
    }else{
        IGMyPaintingListManagerVC *vc=[[IGMyPaintingListManagerVC alloc]initWithType:PAINTINGLIST_MY_TYPE];
        [UIViewController ig_pushViewController:vc animated:YES];
    }
}

- (void)moreClick {
    _cPageNum= [NSString stringWithFormat:@"%d", [_cPageNum intValue] + 1];
    [self createData];
}

- (void)manageLikeClick {
    if (_fDataArr.count==0) {
        [self.tableView presentMessageTips:@"你还没有添加任何喜欢的画单"];
    }else{
        IGMyPaintingListManagerVC *vc=[[IGMyPaintingListManagerVC alloc]initWithType: PAINTINGLIST_FAVORITE_TYPE];
        [UIViewController ig_pushViewController:vc animated:YES];
    }
    
}



@end
